package manong.controller;

import com.github.pagehelper.PageInfo;
import manong.beans.Score;
import manong.beans.User;
import manong.constant.ServerResponse;
import manong.service.IScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/score")
@RestController
public class ScoreController {

    @Autowired
    private IScoreService scoreService;

    @RequestMapping("/findAll")
    public ServerResponse<PageInfo<Score>> findAll(Integer pageNum, Integer pageSize) {
        return scoreService.findAll(pageNum, pageSize);
    }

    @RequestMapping("/findAllBy")
    public ServerResponse<PageInfo<Score>> findBy(Integer sno,
                                                  String className,
                                                  String courseName,
                                                  Integer pageNum,
                                                  Integer pageSize){

        return scoreService.findBy(sno,className,courseName,pageNum,pageSize);
    }

    @RequestMapping("del")
    public ServerResponse<String> del(Integer id) {
        return scoreService.del(id);
    }

    @RequestMapping("/add")
    public ServerResponse<String> add(Integer sno,
                                      Integer score,
                                      String course) {
        return scoreService.add(sno,score,course);
    }

    @RequestMapping("/find")
    public ServerResponse<Score> find(Integer id) {
        return scoreService.find(id);
    }

    @RequestMapping("/update")
    public ServerResponse<String> update(Integer id,
                                         Integer sno,
                                         Integer score,
                                         String courseName) {
        return scoreService.update(id,sno,score,courseName);
    }

    @RequestMapping("/findByStu")
    public ServerResponse<PageInfo<Score>> findByStu(Integer pageNum,
                                                     Integer pageSize,
                                                     HttpSession session){
    	//从session域中获取登录用户，一点自动查询该学生用户的成绩
        User user = (User) session.getAttribute("user"); 
        String username = user.getUsername();//当前学生的姓名
        String Sno = user.getMark();//当前学生的学号，将学号存mark里面，因为学号没有对应的字段
        return scoreService.findByStu(pageNum,pageSize,username,Sno);
    }
}
